from django.db.models import Q, Sum
from django.shortcuts import render
from rest_framework.response import Response

from .models import Like, Like_Comment, Like_Comment_Comment
from .serializer import LikeSerializer, Like_CommentSerializer, Like_Comment_CommentSerializer
from rest_framework import viewsets, mixins, status


# Create your views here.
class LikeViewSet(viewsets.ModelViewSet):
    serializer_class = LikeSerializer

    def get_queryset(self):
        queryset = Like.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset

    def get_like_count(self, request, *args, **kwargs):
        video_id = kwargs.get('pk')  # 从URL参数中获取视频的ID
        user_id = kwargs.get('user')  # 从URL参数中获取user的ID
        filters = Q(video_id=video_id) & Q(is_active=True)
        like_count = Like.objects.filter(filters).count()  # 查询点赞数量
        filters = Q(video_id=video_id) & Q(is_active=True) & Q(user_id=user_id)
        is_liked = len(Like.objects.filter(filters)) != 0
        return Response({'like_count': like_count, 'is_liked': is_liked}, status=status.HTTP_200_OK)


class Like_CommentViewSet(viewsets.ModelViewSet):
    serializer_class = Like_CommentSerializer

    def get_queryset(self):
        queryset = Like_Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset


class Like_Comment_CommentViewSet(viewsets.ModelViewSet):
    serializer_class = Like_Comment_CommentSerializer

    def get_queryset(self):
        queryset = Like_Comment_Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset
